package com.example.blog.service;

import com.example.blog.mapper.BlogMapper;
import com.example.blog.mapper.UserInfoMapper;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogMapper blogMapper;
    public UserInfo selectByName(String userName) {
        return userInfoMapper.selectByName(userName);
    }

    public UserInfo selectById(Integer id) {
        return userInfoMapper.selectById(id);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        BlogInfo blogInfo = blogMapper.selectById(blogId);
        if(blogInfo == null || blogInfo.getUserId() <= 0){
            log.error("获取博客失败, blog: {}", blogId);
            return null;
        }
        return userInfoMapper.selectById(blogInfo.getUserId());
    }
}
